package org.hk.kg.ui;

import java.awt.FlowLayout;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hk.kg.KGUiGloabel;

public class KGStatusBar extends JPanel implements IStatus,Runnable {
	private static Log logger = LogFactory.getLog(KGStatusBar.class);
	private static final long serialVersionUID = 1299780426860836451L;
	private List<String> statusTexts = new ArrayList<String>();
	public KGStatusBar()
	{
		super();
		initailize();
		new Thread(this).start();
	}
	
	private void initailize()
	{
		FlowLayout layout = new FlowLayout();
		layout.setAlignment(FlowLayout.LEFT);
		this.setLayout(layout);
		this.setBorder(KGUiGloabel.BOTTOM_BORDER);
	}

	public synchronized void setStatusText(String text) {
		/**/
		statusTexts.add(text);
		this.notify();
	}
	
	public synchronized String getStatusText() throws InterruptedException
	{
		if(statusTexts.isEmpty())
		{
			this.wait(3000);
		}
		if(statusTexts.isEmpty())
		{
			return "";
		}
		return statusTexts.remove(0);
	}

	public void run() {
		while(true)
		{
			String txt;
			try {
				txt = getStatusText();
			} catch (InterruptedException e) {
				logger.error(e);
				break;
			}
			Graphics2D graphic = (Graphics2D) this.getGraphics();
			graphic.clearRect(0, 2, this.getWidth(), this.getHeight());
			graphic.drawString(txt,5, 12);
		}
	}

	public void putInTask(Runnable thread) {
		// TODO Auto-generated method stub
		
	}
}